using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Media3D;
using _3DHouse.CustomControl;

namespace _3DHouse.Lib
{
    /// <summary>
    /// 移动的撤消重做控制类
    /// </summary>
    class MoveCommand : ICommand
    {
        //private TranslateTransform3D  _Translate;
        /// <summary>
        /// 当前对象
        /// </summary>
        private Cube _UiElement;
        /// <summary>
        /// x轴位移
        /// </summary>
        double _X;
        /// <summary>
        /// z轴位移
        /// </summary>
        double _Z;
        /// <summary>
        /// y轴位移
        /// </summary>
        double _Y;
        /// <summary>
        /// 平移构造
        /// </summary>
        /// <param name="x">x轴位移</param>
        /// <param name="z">z轴位移</param>
        /// <param name="uiElement">移动的对象</param>
        public MoveCommand(double x,double y, double z, Cube uiElement)
        {          
            _UiElement = uiElement;
            _X = x;
            _Z = z;
            _Y = y;
        }
        #region ICommand Members
        /// <summary>
        /// 重做
        /// </summary>
       public void Execute()
        {
          // _Translate=new TranslateTransform3D(_Translate.OffsetX+_X,0,_Translate.OffsetZ+_Z);
         //  Console.WriteLine(""+_X+" "+_Z);
          // Console.WriteLine("" + _Translate.OffsetX + " " + _Translate.OffsetZ);
            Transform3DGroup tfg = _UiElement.Transform as Transform3DGroup;
            foreach (Transform3D tr in tfg.Children)
            {
                if (tr.GetType().Name.Equals("TranslateTransform3D"))
                {//加上移动的数值回到下一步位置
                    TranslateTransform3D _Translate = tr as TranslateTransform3D;
                    _Translate.OffsetZ += _Z;
                    _Translate.OffsetX += _X;
                    _Translate.OffsetY += _Y;
                }
            }
            //_UiElement.Transform = tfg;
            //TranslateTransform3D trans = new TranslateTransform3D();//第一次没有平移
            //_UiElement.Transform = trans;
            //trans.OffsetZ += _Z;
            //trans.OffsetX += _X;
        }
        /// <summary>
       /// 撤消
        /// </summary>
        public void UnExecute()
        {
           // _Translate = new TranslateTransform3D(_Translate.OffsetX - _X, 0, _Translate.OffsetZ - _Z);
         // Console.WriteLine("" + _X + " " + _Z);
          // Console.WriteLine("" + _Translate.OffsetX + " " + _Translate.OffsetZ);
            Transform3DGroup tfg = _UiElement.Transform as Transform3DGroup;
            foreach (Transform3D tr in tfg.Children)
            {
                if (tr.GetType().Name.Equals("TranslateTransform3D"))
                {//减去移动的数值回到上一步位置
                    TranslateTransform3D _Translate = tr as TranslateTransform3D;
                    _Translate.OffsetZ -= _Z;
                    _Translate.OffsetX -= _X;
                    _Translate.OffsetY -= _Y;
                }
            }
            //_UiElement.Transform = tfg;            
            //TranslateTransform3D trans = new TranslateTransform3D();//第一次没有平移
            //_UiElement.Transform = trans;
            //trans.OffsetZ -= _Z;
            //trans.OffsetX -= _X;        
        }

        #endregion
        /// <summary>
        /// 设置前后平移
        /// </summary>
        /// <param name="n">平移值double类型</param>
        //private void SetTranslate()
        //{
        //    if (_UiElement != null)
        //    {
        //        Transform3DGroup tfg = new Transform3DGroup();
        //        //组合变换
        //        if (_UiElement != null && _UiElement.Transform != null && _UiElement.Transform.GetType().Name.Equals("Transform3DGroup"))
        //        {
        //            tfg = _UiElement.Transform as Transform3DGroup;                             
        //            foreach (Transform3D tr in tfg.Children)
        //            {                       
        //                 if (tr.GetType().Name.Equals("TranslateTransform3D"))
        //                {
        //                   // _Translate = tr as TranslateTransform3D;
        //                   //_Translate.OffsetZ += z;
        //                   // _Translate.OffsetX += x;
        //                }
        //            }                    
        //        }
        //        else if (_UiElement != null && _UiElement.Transform != null)
        //        {//单个位移                 
        //           // _Translate = _UiElement.Transform as TranslateTransform3D;
        //           /* _Translate.OffsetZ += z;
        //            _Translate.OffsetX += x;
        //            tfg.Children.Add(_Translate);
        //            _UiElement.Transform = tfg;*/
        //            //_Translate = new TranslateTransform3D(_X,0,_Z);
        //        }
        //    }
        //}
    } 
}
